package com.qztech.sdk.ui.activities;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.util.SparseArray;

import com.qztech.sdk.R;
import com.qztech.sdk.ui.fragment.BaseFragment;
import com.qztech.utils.YzDebug;


public abstract class BaseFragmentActivity extends BaseActivity {
    public static final int SCREEN_ORIENTATION_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
    public static final int SCREEN_ORIENTATION_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    protected SparseArray<BaseFragment> mFragmentSparseArray = null; //Fragment 列表
    protected BaseFragment mCurrentFragment = null;//当前fragment
    //protected BaseFragment mLastFragment = null;//上一个fragment


    public BaseFragment getCurrentFragment() {
        return mCurrentFragment;
    }

    protected FragmentManager mFragmentManager;
    protected FragmentTransaction mFragmentTransaction;


    protected static int fragmentLayoutId = android.R.id.tabcontent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mFragmentManager = getSupportFragmentManager();
        mFragmentTransaction = mFragmentManager.beginTransaction();

    }


    private void ensureFragment() {
        if (mFragmentTransaction == null) {
            mFragmentTransaction = mFragmentManager.beginTransaction();
            //mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        }
    }

    protected void replaceFragment(Fragment fragment) {
        replaceFragment(fragment, false);
    }

    protected void replaceFragment(Fragment newFragment, boolean inout) {
        mFragmentManager.beginTransaction()
                .replace(fragmentLayoutId, newFragment)
                .commit();

    }

    protected void addFragment(Fragment fragment) {
        YzDebug.d("BaseFragmentActivity" + "fragment:" + fragment);
        mFragmentManager.beginTransaction().add(fragmentLayoutId, fragment).commit();
    }

    protected void showAttachFragment(Fragment fragment) {
        if (fragment == null)
            return;
        if (fragment.isDetached()) {
            ensureFragment();
            mFragmentTransaction.setCustomAnimations(
                    R.animator.fragment_slide_right_in, R.animator.fragment_slide_left_out,
                    R.animator.fragment_slide_left_in, R.animator.fragment_slide_right_out);
            mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            mFragmentTransaction.replace(fragmentLayoutId, fragment);
            mFragmentTransaction.attach(fragment);
            mFragmentTransaction.show(fragment);
        } else if (!fragment.isAdded()) {
            ensureFragment();
            mFragmentTransaction.add(fragmentLayoutId, fragment);
            mFragmentTransaction.show(fragment);
        } else {
            ensureFragment();
            mFragmentTransaction.add(fragmentLayoutId, fragment);
            mFragmentTransaction.show(fragment);
        }
    }

    protected void detachFragment(Fragment f) {
        if (f != null && !f.isDetached()) {
            if (mFragmentTransaction == null) {
                mFragmentTransaction = mFragmentManager.beginTransaction();
                mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            }
            mFragmentTransaction.detach(f);
        }
    }




    public  void showDeviceDialog(boolean isHorizontal){}

    public  void showSaveDialog(boolean isH){}
}
